package com.yupi.yupicturebackend.manager.upload;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yupi.yupicturebackend.exception.BusinessException;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.exception.ThrowUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

@Service
public class UrlPictureUpload extends PictureUploadTemplate {
    @Override
    protected void processFile(Object inputSource, File file) {
        String fileUrl = (String) inputSource;
        HttpUtil.downloadFile(fileUrl, file);
    }

    @Override
    protected String getOriginalFilename(Object inputSource) {
        String fileUrl = (String) inputSource;
        HttpResponse httpResponse = null;
        String extName = "";
        try {
            httpResponse = HttpUtil.createRequest(Method.HEAD, fileUrl).execute();
            String contentType = httpResponse.header("Content-Type");
            switch (contentType) {
                case "image/jpeg":
                    extName = "jpg";
                    break;
                case "image/png":
                    extName = "png";
                    break;
                case "image/gif":
                    extName = "gif";
                    break;
                case "image/webp":
                    extName = "webp";
                    break;
                case "image/bmp":
                    extName = "bmp";
                    break;
            }
        } finally {
            if (httpResponse != null) {
                httpResponse.close();
            }
        }
        ThrowUtils.throwIf(StringUtils.isBlank(extName), ErrorCode.PARAMS_ERROR, "文件类型错误");
        return FileUtil.getName(fileUrl) + "." + extName;
    }

    @Override
    protected void validPicture(Object inputSource) {
        String fileUrl = (String) inputSource;
        //1.校验非空
        ThrowUtils.throwIf(StringUtils.isBlank(fileUrl), ErrorCode.PARAMS_ERROR, "文件地址为空");
        //2.校验URL格式
        try {
            new URL(fileUrl);
        } catch (MalformedURLException e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件地址格式错误");
        }
        //3.校验URL协议
        if (!fileUrl.startsWith("http://") && !fileUrl.startsWith("https://")) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件地址协议错误");
        }
        //4.校验文件是否存在
        HttpResponse httpResponse = null;
        try {
            httpResponse = HttpUtil.createRequest(Method.HEAD, fileUrl).execute();
            if (httpResponse.getStatus() != HttpStatus.HTTP_OK)
                return;
            //5.文件类型校验
            String contentType = httpResponse.header("Content-Type");
            final List<String> contentTypeArray = Arrays.asList("image/jpeg", "image/png", "image/gif", "image/jpg", "image/webp", "image/bmp");
            ThrowUtils.throwIf(!contentTypeArray.contains(contentType), ErrorCode.PARAMS_ERROR, "文件类型错误");
            //6.文件大小校验
            String contentLength = httpResponse.header("Content-Length");
            ThrowUtils.throwIf(Long.parseLong(contentLength) > 1024 * 1024 * 10, ErrorCode.PARAMS_ERROR, "文件大小错误");
        } finally {
            if (httpResponse != null)
                httpResponse.close();
        }
    }
}
